package org.jflame.captcha.validator;

import java.io.Serializable;
import java.util.Objects;

/**
 * 表示一个单一值的检验器,直接比较两值是否相等
 * 
 * @author charles.zhang
 * @param <T>
 */
public class SingleVerifiable<T extends Serializable> implements Verifiable<SingleVerifiable<T>> {

    private static final long serialVersionUID = 1L;

    private T value;

    public SingleVerifiable() {
    }

    public SingleVerifiable(T _value) {
        if (_value == null) {
            throw new IllegalArgumentException("argument '_value' must not be null");
        }
        this.value = _value;
    }

    @Override
    public boolean verify(SingleVerifiable<T> part) {
        if (value instanceof String) {
            return value.toString()
                    .equalsIgnoreCase(part.getValue()
                            .toString());
        }
        return Objects.equals(value, part.getValue());
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return value.toString();
    }

}
